package com.gt.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gt.app.common.ColonoscopyEnum;
import com.gt.app.domain.EsCheckReport;
import com.gt.app.domain.EsColonoscopyCheckInfo;
import com.gt.app.domain.EsColonoscopyCheckSymptom;
import com.gt.app.domain.dto.EsCheckReportDto;
import com.gt.app.domain.dto.EsColonoscopyCheckInfoDto;
import com.gt.app.mapper.EsCheckReportMapper;
import com.gt.app.service.EsCheckReportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.app.service.EsColonoscopyCheckSymptomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 检查报告表 服务实现类
 * </p>
 *
 * @author caidesheng
 */
@Slf4j
@Service
public class EsCheckReportServiceImpl extends ServiceImpl<EsCheckReportMapper, EsCheckReport> implements EsCheckReportService {

    @Autowired
    private EsColonoscopyCheckSymptomService esColonoscopyCheckSymptomService;

    @Override
    public List<EsCheckReportDto> getCheckReportById(String checkId) {
        List<EsCheckReportDto> checkInfoList = new ArrayList<>();
        LambdaQueryWrapper<EsCheckReport> eq = new LambdaQueryWrapper<EsCheckReport>().eq(EsCheckReport::getEsCheckId, checkId);
        List<EsCheckReport> checkReports = this.list(eq);
        if (checkReports == null){
            return null;
        }
        for (EsCheckReport checkReport : checkReports) {
            EsCheckReportDto checkReportDto = new EsCheckReportDto();
            BeanUtils.copyProperties(checkReport, checkReportDto);
            LambdaQueryWrapper<EsColonoscopyCheckSymptom> wapper = new LambdaQueryWrapper<EsColonoscopyCheckSymptom>().eq(EsColonoscopyCheckSymptom::getCloOrdrowid, checkReport.getOrdrowid());
            List<EsColonoscopyCheckSymptom> list = esColonoscopyCheckSymptomService.list(wapper);

            List<String> lesionSiteItem = new ArrayList<>();
            List<String> pathologyLesionItem = new ArrayList<>();
            List<String> pathologyIncreaseItem = new ArrayList<>();

            for (EsColonoscopyCheckSymptom esColonoscopyCheckSymptom : list) {
                if (ColonoscopyEnum.LESION_SITE.getName().equals(esColonoscopyCheckSymptom.getType())) {
                    lesionSiteItem.add(esColonoscopyCheckSymptom.getItem());
                }
                if (ColonoscopyEnum.PATHOLOGY_LESION.getName().equals(esColonoscopyCheckSymptom.getType())) {
                    pathologyLesionItem.add(esColonoscopyCheckSymptom.getItem());
                }
                if (ColonoscopyEnum.PATHOLOGY_INCREASE.getName().equals(esColonoscopyCheckSymptom.getType())) {
                    pathologyIncreaseItem.add(esColonoscopyCheckSymptom.getItem());
                }
            }
            checkReportDto.setLesionSiteItem(lesionSiteItem);
            checkReportDto.setPathologyLesionItem(pathologyLesionItem);
            checkReportDto.setPathologyIncreaseItem(pathologyIncreaseItem);
            checkInfoList.add(checkReportDto);
        }
        return checkInfoList;
    }
}
